// 广告路由
const express = require("express");
let router = express.Router();
let { uploading } = require("../utils")  //引入工具
// 引入用户数据库模型
const advModel = require("../models/advmodel.js");

router.get("/getdata", async (req, res) => {

    try {
        let { p } = req.query;
        console.log(p);
        let userData = await userModel.find();
        // 获取对应的页码数据
        let totalCount = userData.length;   //数据的总条数
        // console.log(totalCount);
        let perpage = 2; //每页展示的条数
        let totalpage = Math.ceil(totalCount / perpage);  //页码数

        // 展示的数据
        let showdata = await userModel.find().skip((p - 1) * perpage).limit(perpage)
        res.json({
            info: "获取成功",
            status: 1,
            users: showdata,
            totalpage
        })
    } catch (err) {
        console.log(err);
        res.json({
            info: "获取失败",
            status: 0
        })
    }
})




router.post("/addadv", async (req, res) => {
    try {
        let result = await uploading(req);
        // console.log(result);
        result.time = new Date().toLocaleDateString()  //添加时间
        console.log(result);
        let resobj = await advModel.create(result);   //添加数据
        console.log(resobj);
        res.json({
            info: "添加成功",
            status: 1
        })
    } catch (err) {
        console.log(err);
        res.json({
            info: "添加失败",
            status: 0
        })
    }

})






module.exports = router;